package activity.collection;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ilioili.proabslist.ListStateListener;
import com.ilioili.proabslist.ProAbsListView;
import com.laoxinwen.app.R;

import java.util.ArrayList;

import bean.TabNewsItem;
import bean.wraper.FavNewsListWrapper;
import cn.evun.proabslistview.FreshView;
import cn.evun.proabslistview.LoadView;
import common.BaseActivity;
import common.BaseFragment;
import common.CommonHolder;
import common.FastAdapter;
import common.HttpCallBack;
import common.Requester;
import util.ToastUtil;

/**
 * Created by ilioili on 2015/3/17.
 */
public class FrgNews extends BaseFragment implements ListStateListener {


    private ArrayList<TabNewsItem> dataList = new ArrayList<>();
    private FastAdapter adapter = new FastAdapter() {
        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TabNewsItem tabNewsItem = dataList.get(position);
            BaseActivity activity = (BaseActivity) getActivity();
            CommonHolder.NewsHolder holder = null;

            if (null == convertView) {
                switch (getItemViewType(position)) {
                    case 0:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_no_pic, parent, false);
                        break;
                    case 1:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_one_pic, parent, false);
                        break;
                    case 2:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_three_pics, parent, false);
                        break;
                }
                holder = new CommonHolder.NewsHolder(convertView, activity);
            } else {
                holder = (CommonHolder.NewsHolder) convertView.getTag();
            }
            holder.setData(tabNewsItem);
            return convertView;
        }

        @Override
        public int getItemViewType(int position) {
            TabNewsItem tabNewsItem = dataList.get(position);
            if (null == tabNewsItem.getImgs() || tabNewsItem.getImgs().isEmpty()) {
                return 0;
            } else if (tabNewsItem.getImgs().size() < 3) {
                return 1;
            } else {
                return 2;
            }
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }
    };
    private boolean isFirstCreated = true;
    private ListView listView;
    private int pageIndex;
    private ProAbsListView proAbsListView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        proAbsListView = (ProAbsListView) inflater.inflate(R.layout.fragment_main_item_list, container, false);
        proAbsListView.setListStateListener(this);
//        cardPopupWindow = new CardPopupWindow(getActivity(), imgPool);
        setListView();
        setFreshView();
        setLoadView();
        if (isFirstCreated) {
            isFirstCreated = false;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    proAbsListView.startFresh();
                }
            }, 250);
        }
        return proAbsListView;
    }

    private void setListView() {
        listView = (ListView) proAbsListView.getAbsListView();
        proAbsListView.setLoadable(false);
        proAbsListView.setFreshable(false);
        listView.setAdapter(adapter);
    }

    private void setFreshView() {
        LoadView loadView = new LoadView(getActivity());
        proAbsListView.setLoadView(loadView);
    }

    private void setLoadView() {
        FreshView freshView = new FreshView(getActivity());
        proAbsListView.setFreshView(freshView);
    }

    @Override
    public void onScrollWhileFreshViewVisible(int percentage) {

    }

    @Override
    public void onScrollWhileLoadViewVisible(int percentage) {

    }

    @Override
    public void onStartFreshing() {
        pageIndex = 0;
        requestData();
    }

    @Override
    public void onStartLoading() {
        pageIndex++;
        requestData();
    }


    @Override
    public void setBundle(Bundle bundle) {
        super.setBundle(bundle);
        if (null != proAbsListView) {
            proAbsListView.startFresh();
        }
    }

    private void requestData() {
        Requester.getCollectionNewsList(new HttpCallBack<FavNewsListWrapper>() {
            @Override
            public void onSucceed(FavNewsListWrapper t) {
                FavNewsListWrapper newsListWrapper = t;
                if (null != newsListWrapper.getData()) {
                    if (pageIndex == 0) {
                        dataList.clear();
                    }
                    dataList.addAll(newsListWrapper.getData());
                    if (!dataList.isEmpty()) {
                        if (pageIndex == 0) {
                            listView.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        ToastUtil.showShortToast("暂无收藏的新闻");
                    }
                }
                cancelFreshing();
            }

            @Override
            protected void onNetError() {
                super.onNetError();
                cancelFreshing();
            }

            @Override
            protected void onServerError(FavNewsListWrapper data) {
                super.onServerError(data);
                cancelFreshing();
            }

            private void cancelFreshing() {
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
            }

        });

    }

}
